// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "merchant_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "所属商户",
    "label": "所属商户"
  },
  "company_license_icon": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 1
      }
    ],
    "title": "经营许可证",
    "label": "经营许可证"
  },
  "company_license_type": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "食品经营许可证",
            "value": 0
          },
          {
            "text": "旅行社业务经营许可证",
            "disable": true,
            "value": 1
          },
          {
            "text": "人力资源许可证",
            "disable": true,
            "value": 2
          },
          {
            "text": "人力资源许可证",
            "disable": true,
            "value": 3
          }
        ]
      }
    ],
    "title": "许可证件类型",
    "defaultValue": 0,
    "label": "许可证件类型"
  },
  "company_approval_date": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "核准日期",
    "label": "核准日期"
  },
  "company_license_valid_date": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "有效期至",
    "label": "有效期至"
  },
  "company_license_code": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "许可证编号",
    "label": "许可证编号"
  },
  "company_license_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "经营者名称",
    "label": "经营者名称"
  },
  "company_license_credit_code": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "统一社会信用代码",
    "label": "统一社会信用代码"
  },
  "company_license_holder_name": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "法定代表人/负责人",
    "label": "法定代表人/负责人"
  },
  "company_license_residence": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "住所",
    "label": "住所"
  },
  "company_license_work_location": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "经营场所",
    "label": "经营场所"
  },
  "company_license_main_business": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "主体业态",
    "label": "主体业态"
  },
  "company_license_business_scope": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "经营项目/范围",
    "label": "经营项目/范围"
  }
}

const enumConverter = {
  "company_license_type_valuetotext": {
    "0": "食品经营许可证",
    "1": "旅行社业务经营许可证",
    "2": "人力资源许可证",
    "3": "人力资源许可证"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
